﻿using BCCommon;
using BCCommon.Enums;
using BCEntity.Common.PushMessageConfig;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BCData.Common.PushMessageConfig
{
    public class PushMessageConfigData : IPushMessageConfigData
    {
        private const string QUERY = @"SELECT PushMessageConfigId,PushMessageType,PushMessageName,Ranges,MessageTitle,MessageTemplate,MessageTemplateRemark,IsSystem,
                                        CreateTime,UpdateTime,Editer
                                        FROM PushMessageConfig";

        protected readonly IDatabaseContext database;

        public PushMessageConfigData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<PushMessageConfigEntity> GetEntity(PushMessageType pushMessageType)
        {
            string sql = string.Format("{0} WHERE PushMessageType=?PushMessageType", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PushMessageType", pushMessageType.ToString(), DbType.AnsiString);
            return await database.Slave.QueryFirstOrDefaultAsync<PushMessageConfigEntity>(sql, parameters, database.Transaction);
        }

        public async Task<PushMessageConfigEntity> Add(PushMessageConfigEntity pushMessageConfigEntity)
        {
            string sql = SqlCommands.GetInsertSql("PushMessageConfig", new string[] { "PushMessageType", "PushMessageName", "Ranges",
                "MessageTitle", "MessageTemplate", "MessageTemplateRemark","IsSystem","Editer" });
            sql = string.Format("{0} {1} WHERE PushMessageConfigId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PushMessageType", pushMessageConfigEntity.PushMessageType);
            parameters.Add("?PushMessageName", pushMessageConfigEntity.PushMessageName);
            parameters.Add("?Ranges", pushMessageConfigEntity.Ranges);
            parameters.Add("?MessageTitle", pushMessageConfigEntity.MessageTitle);
            parameters.Add("?MessageTemplate", pushMessageConfigEntity.MessageTemplate);
            parameters.Add("?MessageTemplateRemark", pushMessageConfigEntity.MessageTemplateRemark);
            parameters.Add("?IsSystem", false);
            parameters.Add("?Editer", pushMessageConfigEntity.Editer);
            return await database.Slave.QueryFirstOrDefaultAsync<PushMessageConfigEntity>(sql, parameters);
        }

        public async Task<object> CheckPushMessageConfigExists(int pushMessageConfigId)
        {
            string sql = @"SELECT 1 FROM PushMessageConfig WHERE PushMessageConfigId=?PushMessageConfigId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PushMessageConfigId", pushMessageConfigId, DbType.Int32, size: 10);
            return await database.Slave.ExecuteScalarAsync(sql, parameters);
        }

        public async Task<object> CheckPushMessageConfigExists(PushMessageType pushMessageType)
        {
            string sql = @"SELECT 1 FROM PushMessageConfig WHERE PushMessageType=?PushMessageType;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PushMessageType", pushMessageType.ToString(), DbType.AnsiString, size: 30);
            return await database.Slave.ExecuteScalarAsync(sql, parameters);
        }

        public async Task<object> CheckPushMessageConfigExists(int pushMessageConfigId, PushMessageType pushMessageType)
        {
            string sql = @"SELECT 1 FROM PushMessageConfig WHERE PushMessageType=?PushMessageType AND PushMessageConfigId=?PushMessageConfigId;";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PushMessageType", pushMessageType.ToString(), DbType.AnsiString, size: 30);
            parameters.Add("?PushMessageConfigId", pushMessageConfigId, DbType.Int32, size: 10);
            return await database.Slave.ExecuteScalarAsync(sql, parameters);
        }

        public async Task<PushMessageConfigEntity> Update(PushMessageConfigEntity pushMessageConfigEntity)
        {
            string sql = SqlCommands.GetUpdateSql("PushMessageConfig", new string[] { "PushMessageType", "PushMessageName", "Ranges",
                "MessageTitle", "MessageTemplate", "MessageTemplateRemark","Editer" }, new string[] { "PushMessageConfigId" });
            sql = string.Format("{0} {1} WHERE PushMessageConfigId=?PushMessageConfigId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PushMessageType", pushMessageConfigEntity.PushMessageType);
            parameters.Add("?PushMessageName", pushMessageConfigEntity.PushMessageName);
            parameters.Add("?Ranges", pushMessageConfigEntity.Ranges);
            parameters.Add("?MessageTitle", pushMessageConfigEntity.MessageTitle);
            parameters.Add("?MessageTemplate", pushMessageConfigEntity.MessageTemplate);
            parameters.Add("?MessageTemplateRemark", pushMessageConfigEntity.MessageTemplateRemark);
            parameters.Add("?IsSystem", false);
            parameters.Add("?PushMessageConfigId", pushMessageConfigEntity.PushMessageConfigId);
            parameters.Add("?Editer", pushMessageConfigEntity.Editer);
            return await database.Slave.QueryFirstOrDefaultAsync<PushMessageConfigEntity>(sql, parameters);
        }

        public async Task<PushMessageConfigEntity> GetById(int pushMessageConfigId)
        {
            var sql = string.Format("{0} WHERE PushMessageConfigId=?PushMessageConfigId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PushMessageConfigId", pushMessageConfigId);
            return await database.Slave.QueryFirstOrDefaultAsync<PushMessageConfigEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<PushMessageConfigEntity>, int)> Query(DateTime? start, DateTime? end, bool? ascending, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime {4} LIMIT {2},{3};
                               SELECT Count(1) FROM PushMessageConfig {1}",
                  QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex - 1) * pageSize,
                  pageSize,
                  ascending.HasValue && ascending.Value ? "ASC" : "DESC");

            return await database.Slave.QueryPageableListAsync<PushMessageConfigEntity>(sql, parameters);
        }
    }
}